#include "customercontroller.h"
#include "core/customermodel.h"
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>

CustomerController::CustomerController(QObject *parent) :
    BaseController(parent)
{
}

void CustomerController::updateCustomer(CustomerModel *customer)
{
    QString sql;
    QSqlQuery query;

    sql.append("UPDATE Customers "
               "SET name=:name, "
               "address=:address, "
               "contactNumber=:contactNumber, "
               "notes=:notes "
               "WHERE id=:id");
    query.prepare(sql);
    query.bindValue(":name", customer->getName());
    query.bindValue(":address", customer->getAddress());
    query.bindValue(":contactNumber", customer->getContactNumber());
    query.bindValue(":notes", customer->getNote());
    query.bindValue(":id", customer->getID());

    bool ok = query.exec();

    if(!ok) {
        qDebug() << "CustomerController::updateCustomer Query failed!" << query.lastError().text();
        qDebug() << "ID is: " << customer->getID();
    }
}

void CustomerController::addCustomer(CustomerModel *customer)
{
    QString sql;
    QSqlQuery query;

    sql.append("INSERT INTO Customers "
               "(name, address, contactNumber, notes) "
               "values (:name, :address, :contactNumber, :notes)");
    query.prepare(sql);
    query.bindValue(":name", customer->getName());
    query.bindValue(":address", customer->getAddress());
    query.bindValue(":contactNumber", customer->getContactNumber());
    query.bindValue(":notes", customer->getNote());

    bool ok = query.exec();

    if(!ok) {
        qDebug() << "CustomerController::addCustomer Query failed!" << query.lastError().text();
    }
}

QList<BaseModel *> CustomerController::getAll()
{
    QList<BaseModel *> list;
    CustomerModel *customer;
    QSqlQuery query;

    bool ok = query.exec("SELECT ID, name, address, contactNumber, notes FROM Customers");

    if (!ok) {
            /* TODO: Print detailed error here */
            qDebug() << "CustomerController: getAll() query failed!"
                        << query.lastError().text();
    }

    while (query.next()) {
            customer = new CustomerModel();
            qDebug() << "CustomerController: Got customer with ID " << query.value(0).toInt();
            customer->setID(query.value(0).toInt());
            customer->setName(query.value(1).toString());
            customer->setAddress(query.value(2).toString());
            customer->setContactNumber(query.value(3).toString());
            customer->setNote(query.value(4).toString());

            list.append(customer);
    }

    emit update(list);
    return list;
}

CustomerModel *CustomerController::getCustomerByName(QString name)
{
    QSqlQuery query;
    QString sql;
    CustomerModel *customer;
    bool ok;

    sql = "SELECT ID, address, contactNumber, notes FROM Customers "
            "WHERE name = :name";
    query.prepare(sql);
    query.bindValue(":name", name);

    ok = query.exec();

    if (query.next()) {
        customer = new CustomerModel;
        customer->setID(query.value(0).toInt());
        customer->setName(name);
        customer->setAddress(query.value(1).toString());
        customer->setContactNumber(query.value(2).toString());
        customer->setNote(query.value(3).toString());

        return customer;
    }

    return NULL;
}

